package org.csu.canteen.controller;

import org.csu.canteen.entity.Item;
import org.csu.canteen.entity.Languages;
import org.csu.canteen.entity.Product;
import org.csu.canteen.service.CatalogService;
import org.csu.canteen.vo.CategoryVO;
import org.csu.canteen.vo.ItemVO;
import org.csu.canteen.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.DecimalFormat;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @GetMapping("index")
    public String index(@RequestParam(required = false) String username, Model model) {
        if (username != null && !username.isEmpty()) {
            model.addAttribute("username", username);
        }
        return "catalog/main"; // 返回视图
    }

    @GetMapping("viewCategory")
    public String viewCategory(String categoryId, Model model) {
        CategoryVO categoryVO = catalogService.getCategory(categoryId);
        List<ProductVO> productList = catalogService.getProductsByCategory(categoryId);

        model.addAttribute("category", categoryVO);
        model.addAttribute("productList", productList);

        return "catalog/category";
    }

    @GetMapping("viewProduct")
    public String viewProduct(@RequestParam(required = false)String categoryId,String productId, Model model) {
        CategoryVO categoryVO = catalogService.getCategory(categoryId);
        ProductVO productVO = catalogService.getProduct(productId);
        List<Item> itemList = catalogService.getProduct(productId).getItemList();


        model.addAttribute("category", categoryVO);
        model.addAttribute("product", productVO);
        model.addAttribute("itemList", itemList);

        return "catalog/product";  // 对应 Thymeleaf 视图
    }
    // 显示单个物品的详细信息
    @GetMapping("/viewItem")
    public String viewItem(String categoryId,String itemId, Model model) {

        //获取物品和商品的详细信息
        ItemVO itemVO = catalogService.getItem(itemId); // 获取物品信息
        ProductVO productVO = catalogService.getProduct(itemVO.getProductId()); // 获取商品信息
        CategoryVO categoryVO = catalogService.getCategory(categoryId);

        DecimalFormat df = new DecimalFormat("#,##0.00");
        String formattedPrice = df.format(itemVO.getListPrice());
        model.addAttribute("formattedPrice", formattedPrice);


        // 将数据添加到模型中，以便传递给 Thymeleaf 页面
        model.addAttribute("category", categoryVO);
        model.addAttribute("item", itemVO);
        model.addAttribute("product", productVO);

        // 返回视图路径
        return "catalog/item";  // 对应的 Thymeleaf 视图
    }
    // 显示用户的收藏列表
    @GetMapping("/includeMyList")
    public String includeMyList(@RequestParam(value = "productId", required = false) String productId,
                                @RequestParam(value = "itemId", required = false) String itemId,
                                Model model) {

        // 获取商品和物品的详细信息
        ProductVO productVO = catalogService.getProduct(productId);  // 获取商品信息
        ItemVO itemVO = catalogService.getItem(itemId);  // 获取物品信息
        List<Item> itemList = catalogService.getProduct(productId).getItemList();  // 获取商品下所有的物品列表

        // 将数据添加到模型中，以便传递给 Thymeleaf 页面
        model.addAttribute("product", productVO);
        model.addAttribute("item", itemVO);
        model.addAttribute("itemList", itemList);

        // 返回视图路径
        return "catalog/includeMyList";  // 对应的 Thymeleaf 视图
    }

    @GetMapping("/selectLanguage")
    public String selectLanguage(Model model) {
        List<Languages> languages = catalogService.getAllLanguages(); // 这里调用 Service 层获取语言列表
        model.addAttribute("languages", languages);
        return "catalog/languages"; // 对应 Thymeleaf 视图
    }

    @GetMapping("/search")
    public String searchProducts(@RequestParam(value = "keywords", required = false, defaultValue = "") String keyword, Model model) {
        // 查询符合条件的商品
        List<Product> productList = catalogService.searchProductList(keyword);

        // 传递数据到前端
        model.addAttribute("productList", productList);
        model.addAttribute("searchKeyword", keyword); // 让搜索框能保留输入的关键词

        return "catalog/searchProducts"; // 返回搜索结果页面
    }

}
